﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StorageBox
{
    public class ElementFactoryManager : ManagerBase
    {
        private static ElementFactoryManager _elementFactoryManager;

        private Dictionary<Type, ElementFactory> _elementFactorys = new Dictionary<Type, ElementFactory>();

        public ElementFactoryManager()
        {
            _elementFactoryManager = this;

            //添加元素工厂
            _elementFactorys.Add(typeof(ApplyElement), new ApplyElementFactory());
            _elementFactorys.Add(typeof(AddApplyElement), new AddApplyFactory());



            //取出临时元素
            AddApplyFactory.SetViewElement(BoxApplication.GetMainWondw());
        }

        public ElementFactory GetFactory<TElement>()
        {
            return _elementFactorys[typeof(TElement)];
        }

        /// <summary>
        /// 创建元素
        /// </summary>
        public static TElement Create<TElement, TData>(TData data)
            where TElement : FrameworkElement, new()
            where TData : ElementData
        {
            var list = _elementFactoryManager._elementFactorys;
            if (list.ContainsKey(typeof(TElement)))
            {
                return ((dynamic)list[typeof(TElement)]).Create(data);
            }
            else
            {
                return new TElement();
            }

        }

    }
}
